﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;
using Sio.Mdm.Entities;
using Sio.Mdm.Client;
using Sio.Mdm.BF.Idm;
using Sio.Mdm.BF.Sifranti;
using Sio.Mdm.Types.Database;
using Sio.Mdm.Types;
using Sio.Mdm.BF.Ldap;
using System.Collections.Generic;

namespace Sio.Mdm.Web.Instalacija
{
	public partial class NaziviUciteljevSeznam : Controls.Page
	{
		#region Definitions
		public interface SplosnoRowEditData
		{
			int NazivUciteljaId { get; set; }
			string Naziv { get; set; }
		}
		#endregion

		#region OnLoad
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			NaloziSeznam();
		}
		#endregion

		#region Editing
		protected void UrediPodatke(Object sender, DirectEventArgs e)
		{
			SplosnoRowEditData EditData = XML.Deserialize<SplosnoRowEditData>(e.ExtraParams["data"]);
			if (EditData.NazivUciteljaId == -99)
			{
				NazivUcitelja ent = EntityFactory.CreateInstance<NazivUcitelja>();
				ent.Naziv = EditData.Naziv;
				ent.Update();
			}
			else if (EditData.NazivUciteljaId > 0)
			{
				NazivUcitelja ent = EntityFactory.LoadEntity<NazivUcitelja>
				 (new Filter<NazivUcitelja>().Where(r => r.NazivUciteljaId == EditData.NazivUciteljaId)
					);

				if (ent.EntityState != EntityState.Unknown)
				{
					ent.Naziv = EditData.Naziv;
					ent.Update();
				}
				else
				{
					e.Success = false;
					X.Msg.Alert("Manjkajoči podatki", "Iskani zapis ne obstaja.").Show();
					return;
				}
			}
		}
		protected void OdstraniZapise(object sender, DirectEventArgs e)
		{
			List<int> deletedItems = e.GetIdList<int>("selected", "NazivUciteljaId");
			if (deletedItems.Count > 0)
			{
				EntityCollection<NazivUcitelja> col = new EntityCollection<NazivUcitelja>(
					new Filter<NazivUcitelja>().SelectByField(r => r.NazivUciteljaId, deletedItems.ToArray())
				);
				col.RemoveAll();
				col.Update();
			}
		}
		#endregion

		#region Private
		private void NaloziSeznam()
		{
			GridSplosniSeznam.LoadData<NazivUcitelja>(
				new Filter<NazivUcitelja>().Where(r => r.NazivUciteljaId != null));
		}
		#endregion
	}
}
